package cn.schoolwow.workflow.module.definition.action.flow;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDeployRequest;
import cn.schoolwow.workflow.entity.WorkFlowDefinition;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;

public class CheckWorkFlowDeployRequestFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkName(flowContext);
        checkWorkFlowDeployExistFlow(flowContext);
        parseByWorkFlowDeployRequest(flowContext);
        parseByInputStream(flowContext);
        if(!flowContext.containKey("workflow")){
            throw new IllegalArgumentException("工作流部署文件不存在");
        }
    }

    @Override
    public String name() {
        return "检查工作流部署请求";
    }

    private void checkName(FlowContext flowContext){
        WorkFlowDeployRequest workFlowDeployRequest = flowContext.checkInstanceData(WorkFlowDeployRequest.class);

        CheckInstance.newCheck()
                .notEmptyCheck(workFlowDeployRequest.name, "工作流定义名称");
        if(null==workFlowDeployRequest.inputStream&&null==workFlowDeployRequest.workFlowDeployFileContext){
            throw new IllegalArgumentException("部署输入流和部署文件上下文不能都为空");
        }
    }

    private void checkWorkFlowDeployExistFlow(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        WorkFlowDeployRequest workFlowDeployRequest = flowContext.checkInstanceData(WorkFlowDeployRequest.class);

        dao.addRecordString("部署工作流-判断工作流实例是否已经部署");
        long count = dao.query(WorkFlowDefinition.class)
                .addQuery("name", workFlowDeployRequest.name)
                .execute()
                .count();
        if(count>0){
            throw new IllegalArgumentException("该流程已经部署过!名称:"+workFlowDeployRequest.name);
        }
    }

    private void parseByWorkFlowDeployRequest(FlowContext flowContext){
        WorkFlowDeployRequest workFlowDeployRequest = flowContext.checkInstanceData(WorkFlowDeployRequest.class);

        if(null==workFlowDeployRequest.workFlowDeployFileContext){
            return;
        }
        if(flowContext.containInstanceKey(WorkFlowDeployRequest.class)){
            String content = JSON.toJSONString(workFlowDeployRequest.workFlowDeployFileContext);
            JSONObject workflow = JSONObject.parseObject(content);
            flowContext.putTemporaryData("workflow", workflow);
        }
    }

    private void parseByInputStream(FlowContext flowContext) throws Exception{
        WorkFlowDeployRequest workFlowDeployRequest = flowContext.checkInstanceData(WorkFlowDeployRequest.class);

        if(null==workFlowDeployRequest.inputStream){
            return;
        }
        String content = IOUtils.toString(workFlowDeployRequest.inputStream, StandardCharsets.UTF_8);
        if(StringUtils.isBlank(content)){
            throw new IllegalArgumentException("流程定义文件内容为空!名称:"+workFlowDeployRequest.name);
        }
        JSONObject workflow = JSONObject.parseObject(content);
        flowContext.putTemporaryData("workflow", workflow);
    }
}
